package com.hq.ims.platform.controller;

import com.hq.ims.data.annotation.UserOperationMonitor;
import com.hq.ims.data.entity.LogSysAccess;
import com.hq.ims.data.entity.LogSysException;
import com.hq.ims.data.entity.LogUserOperation;
import com.hq.ims.data.service.BaseLogSysAccessService;
import com.hq.ims.data.service.BaseLogSysExceptionService;
import com.hq.ims.data.service.BaseLogUserLoginInfoService;
import com.hq.ims.data.service.BaseLogUserOperationService;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.vo.LogSysAccessMapstruct;
import com.hq.ims.data.vo.LogSysExceptionMapstruct;
import com.hq.ims.data.vo.LogUserLoginInfoMapstruct;
import com.hq.ims.data.vo.LogUserOperationMapstruct;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.mybatis.base.page.ReturnPage;
import com.hq.utils.vo.ReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限中心controller
 */
@Api(tags = "操作日志")
@RestController
@RequestMapping(LocalConstant.SYS_TYPE_PLATFORM_URL_PREFIX + "/operateLog")
public class PlatformOperateLogController {

    @Resource
    private BaseLogUserOperationService baseLogUserOperationService;
    @Resource
    private BaseLogSysAccessService baseLogSysAccessService;
    @Resource
    private BaseLogSysExceptionService baseLogSysExceptionService;
    @Resource
    private BaseLogUserLoginInfoService baseLogUserLoginInfoService;

    //-------------------------- 用户操作日志 --------------------------

    @ApiOperation(value = "用户日志分页", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/userOperateLog/getByPage", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<ReturnPage<LogUserOperation>> userOperateLogGetByPage(
            @RequestBody RequestPage page,
            @RequestBody LogUserOperationMapstruct.PageSearchVo searchVo
    ) {
        List<LogUserOperation> dataList = baseLogUserOperationService.getPage(page, searchVo);
        return ReturnVo.buildSuccess(page.getReturnPage());
    }

    @ApiOperation(value = "清除用户操作日志", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/userOperateLog/clearLogByTime", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> userOperateLogClearLogByTime(
            @RequestBody LogUserOperationMapstruct.ClearLogVo clearVo
    ) {
        baseLogUserOperationService.clearLogByTime(clearVo);
        return ReturnVo.buildSuccess(true);
    }


    //-------------------------- 系统访问日志 --------------------------
    @ApiOperation(value = "获取系统访问日志列表", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping("/sysAccess/getByPage")
    public ReturnVo<ReturnPage<LogSysAccess>> sysAccessGetByPage(
            @RequestBody RequestPage page,
            @RequestBody LogSysAccessMapstruct.PageSearchVo searchData
    ) {
        baseLogSysAccessService.getByPage(page, searchData);
        return ReturnVo.buildSuccess(page.getReturnPage());
    }

    @ApiOperation(value = "根据时间区间清除系统访问日志", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping("/sysAccess/clearLogByTime")
    public ReturnVo<Boolean> sysAccessClearLogByTime(@RequestBody LogSysAccessMapstruct.ClearLogVo clearLogVo) {
        baseLogSysAccessService.clearLogByTime(clearLogVo);
        return ReturnVo.buildSuccess(true);
    }

    //-------------------------- 系统异常日志 --------------------------
    @ApiOperation(value = "获取系统异常日志列表", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping("/sysException/getByPage")
    public ReturnVo<ReturnPage<LogSysException>> sysExceptionGetByPage(
            @RequestBody RequestPage page,
            @RequestBody LogSysExceptionMapstruct.PageSearchVo searchData
    ) {
        baseLogSysExceptionService.getByPage(page, searchData);
        return ReturnVo.buildSuccess(page.getReturnPage());
    }

    @ApiOperation(value = "根据时间区间清除系统访问日志", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping("/sysException/clearLogByTime")
    public ReturnVo<Boolean> sysExceptionClearLogByTime(@RequestBody LogSysExceptionMapstruct.ClearLogVo clearLogVo) {
        baseLogSysExceptionService.clearLogByTime(clearLogVo);
        return ReturnVo.buildSuccess(true);
    }


    //-------------------------- 用户登陆日志 --------------------------

    @ApiOperation(value = "获取系统异常日志列表", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping("/userLogin/getByPage")
    public ReturnVo<ReturnPage<LogSysException>> userLoginGetByPage(
            @RequestBody RequestPage page,
            @RequestBody LogUserLoginInfoMapstruct.PageSearchVo searchData
    ) {
        baseLogUserLoginInfoService.getByPage(page, searchData);
        return ReturnVo.buildSuccess(page.getReturnPage());
    }

    @ApiOperation(value = "根据时间区间清除系统访问日志", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping("/userLogin/clearLogByTime")
    public ReturnVo<Boolean> userLoginClearLogByTime(@RequestBody LogUserLoginInfoMapstruct.ClearLogVo clearLogVo) {
        baseLogUserLoginInfoService.clearLogByTime(clearLogVo);
        return ReturnVo.buildSuccess(true);
    }

}
